<?php

namespace App\Http\ApiControllers;

use App\Facades\Api;
use App\Http\Resources\LikeCollection;
use App\Models\Player;
use Exception;
use Illuminate\Http\JsonResponse;

class LikeController extends ApiController
{
    public function index(Player $player): JsonResponse
    {
        $list = $player->likes()
            ->latest()
            ->paginate();

        return $this->success(new LikeCollection($list));
    }

    /**
     * Notes   : 点赞
     *
     * @Date   : 2024/7/30 上午10:16
     * @Author : <Jason.C>
     * @param  Player  $player
     * @return JsonResponse
     */
    public function store(Player $player): JsonResponse
    {
        try {
            $this->verifyCaptcha();
            $player->giveLike(Api::user());

            return $this->success();
        } catch (Exception $e) {
            return $this->failed($e->getMessage(), $e->getCode());
        }
    }
}
